﻿#region File and License Information
/*
<File>
	<License Type="BSD">
		Copyright © 2009 - 2012, Daniel Vaughan. All rights reserved.
	
		This file is part of Calcium (http://calciumsdk.net).

		Redistribution and use in source and binary forms, with or without
		modification, are permitted provided that the following conditions are met:
			* Redistributions of source code must retain the above copyright
			  notice, this list of conditions and the following disclaimer.
			* Redistributions in binary form must reproduce the above copyright
			  notice, this list of conditions and the following disclaimer in the
			  documentation and/or other materials provided with the distribution.
			* Neither the name of the <organization> nor the
			  names of its contributors may be used to endorse or promote products
			  derived from this software without specific prior written permission.

		THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
		ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
		WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
		DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
		DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
		(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
		LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
		ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
		(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
		SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
	</License>
	<Owner Name="Daniel Vaughan" Email="danielvaughan@outcoder.com" />
	<CreationDate>2009-04-15 21:47:56Z</CreationDate>
</File>
*/
#endregion

using System;
using System.IO;
using System.Windows;
using System.Windows.Threading;

using Microsoft.Win32;

using log4net;

using DanielVaughan.ComponentModel;
using DanielVaughan.Data;
using DanielVaughan.Gui;
using DanielVaughan.IO;
using DanielVaughan.Resources;

namespace DanielVaughan.Services
{
	/// <summary>
	/// Default implementation of <see cref="IFileService"/>.
	/// </summary>
	public class FileService : IFileService
	{
		static readonly ILog log = LogManager.GetLogger(typeof(FileService));

		public FileOperationResult SaveAs(string fileName, FileOperationHandler operationHandler,
			FileErrorAction fileErrorAction)
		{
			ArgumentValidator.AssertNotNull(operationHandler, "operationHandler");

			var shell = Dependency.Resolve<IMainWindow>() as Window;
			var dialog = new SaveFileDialog();
			/* TODO: Customize the save dialog. */
			dialog.FileName = fileName;
			if (!dialog.ShowDialog(shell).Value)
			{
				return FileOperationResult.Cancelled;
			}
			return Save(dialog.FileName, operationHandler, fileErrorAction);
		}

		public FileOperationResult Save(string fileName, FileOperationHandler operationHandler, 
			FileErrorAction fileErrorAction)
		{
			ArgumentValidator.AssertNotNullOrEmpty(fileName, "fileName");
			ArgumentValidator.AssertNotNull(operationHandler, "operationHandler");

			FileErrorAction actualFileErrorAction = fileErrorAction;

			string userMessage = null;
			bool ioExceptionOccured = false;
			try
			{
				operationHandler(fileName);
				return FileOperationResult.Successful;
			}
			catch (IOException ex)
			{
				log.Info("Unable to save file: " + fileName, ex);
				userMessage = "A problem occured saving the file."; /* TODO: Make localizable resource. */
				ioExceptionOccured = true;
			}
			catch (Exception ex) /* TODO: catch common IO errors and report to user. */
			{
				log.Info("Unable to save file: " + fileName, ex);
				var userMessageException = ex as IUserMessageProvider;
				if (userMessageException != null && userMessageException.UserMessagePresent)
				{
					userMessage = userMessageException.UserMessage;
				}
			}

			if (!ioExceptionOccured)
			{
				if (fileErrorAction == FileErrorAction.UseAlternative)
				{	/* If this wasn't an IOException then we do not allow the user to select an alternative. */
					actualFileErrorAction = FileErrorAction.InformOnly;
				}
			}

			var result = FileOperationResult.Failed;

			var messageService = Dependency.Resolve<IMessageService>();

			switch (actualFileErrorAction)
			{
				case FileErrorAction.UseAlternative:
					messageService.ShowError(userMessage, StringResources.Services_FileService_UnableToSaveFile);
					do
					{
						result = SaveAs(fileName, operationHandler, FileErrorAction.InformOnly);
					} while (result != FileOperationResult.Successful && result != FileOperationResult.Cancelled);
					break;
				case FileErrorAction.InformOnly:
					messageService.ShowError(userMessage, StringResources.Services_FileService_UnableToSaveFile);
					break;
			}
			return result;
		}

		public FileOperationResult Open(FileOperationHandler operationHandler,
			FileErrorAction fileErrorAction, string dialogFilter)
		{
			ArgumentValidator.AssertNotNull(operationHandler, "operationHandler");
			return Open(null, operationHandler, fileErrorAction, dialogFilter);
		}

		public FileOperationResult Open(string filePath, FileOperationHandler operationHandler,
			FileErrorAction fileErrorAction, string dialogFilter)
		{
			ArgumentValidator.AssertNotNull(operationHandler, "operationHandler");

			string fileNameToUse = filePath;
			if (!File.Exists(filePath))
			{
				var shell = Dependency.Resolve<IMainWindow>() as Window;
				FileOperationResult result = FileOperationResult.Successful;
				var dispatcher = Dependency.Resolve<Dispatcher>();
				dispatcher.InvokeIfRequired(delegate
				{
					var dialog = new OpenFileDialog();
					/* TODO: Customize the save dialog. */
					dialog.Title = "Choose file name";
					dialog.Filter = dialogFilter;
					if (!dialog.ShowDialog(shell).Value)
					{
						result = FileOperationResult.Cancelled;
						return;
					}
					fileNameToUse = dialog.FileName;
				});
				if (result != FileOperationResult.Successful)
				{
					return result;
				}
			}

			string userMessage = null;
			try
			{
				operationHandler(fileNameToUse);
				return FileOperationResult.Successful;
			}
			catch (MementoIncompatibilityException ex)
			{
				userMessage = string.Format(StringResources.Services_FileService_FileCreatedInNewerVersion, fileNameToUse);
				log.Info("Unable to open file: " + filePath, ex);
			}
			catch (Exception ex) /* TODO: catch common IO errors and report to user. */
			{
				log.Info("Unable to open file: " + filePath, ex);
			}

			if (userMessage == null)
			{
				userMessage = string.Format(StringResources.Services_FileService_UnableToOpenFile, fileNameToUse);
			}

			var messageService = Dependency.Resolve<IMessageService>();

			switch (fileErrorAction)
			{
				case FileErrorAction.UseAlternative:
					messageService.ShowWarning(userMessage);
					var result = Open(null, operationHandler, FileErrorAction.UseAlternative, dialogFilter);
					return result;
				case FileErrorAction.InformOnly:
					messageService.ShowWarning(userMessage);
					break;
			}
			return FileOperationResult.Failed;
		}

	}
}
